package utils;

import java.io.*;
import java.util.List;

public class QfFileAppender {
    private String filepath;
    BufferedWriter bw;

    public QfFileAppender(String filepath) {
        this.filepath = filepath;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath, true)));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public Integer appendListToFile(List<String> data) {
        Integer count = 0;
        try {
            for (String line : data) {
                bw.write(line);
                bw.newLine();
                count++;
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return count;
    }
    public void close(){
        try {
            bw.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
